package com.kaoker.system.dao.impl;

import java.util.List;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.kaoker.system.dao.IExamLevelDao;
import com.kaoker.system.entity.ExamLevel;

public class ExamLevelDao extends HibernateDaoSupport implements IExamLevelDao {
	@Override
	public void addLevel(ExamLevel level) {
		if (ObjectUtils.equals(level, null))
			return;

		this.getHibernateTemplate().save(level);
	}

	@Override
	public void updateLevel(ExamLevel level) {
		if (ObjectUtils.equals(level, null))
			return;

		this.getHibernateTemplate().update(level);
	}

	@Override
	public List<ExamLevel> getAllLevels() {
		return this.getHibernateTemplate().find("from ExamLevel");
	}

	/**
	 * 根据name, 获取ExamLevel
	 */
	@Override
	public ExamLevel getExamLevel(String name) {
		if (StringUtils.isBlank(name)) return null;

		List<ExamLevel> list = (List<ExamLevel>)this.getHibernateTemplate().find(
				"from ExamLevel where name=?", name);

		return (list.size() > 0) ? list.get(0) : null;
	}

	/**
	 * Id indicates one record uniquely.
	 * 
	 * @id unique id in DB.
	 */
	@Override
	public ExamLevel getExamLevel(Long id) {
		return this.getHibernateTemplate().load(ExamLevel.class, id);
	}

}
